﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlDataAccess;

namespace QuanLyBanHang.BaoCao
{
    public partial class F980_Thong_ke_doanh_thu_theo_nhan_vien : Form
    {
        public F980_Thong_ke_doanh_thu_theo_nhan_vien()
        {
            InitializeComponent();
            set_define_events();
        }

        #region Members
        BKIQuanLyBanHangEntities db = new BKIQuanLyBanHangEntities();
        #endregion

        #region Private Methods

        private void set_define_events()
        {
            this.Load += F980_Thong_ke_doanh_thu_theo_nhan_vien_Load;
            this.m_cmd_xem_bao_cao.Click +=m_cmd_xem_bao_cao_Click;
        }

        private void load_data_to_grid()
        {
            var v_lst_chi_tiet_hoa_don_ban = db.GD_CT_HOA_DON_BAN
                .Where(x =>
                    (x.GD_HOA_DON_BAN.NGAY_BAN >= m_dtp_tu_ngay.Value
                    && x.GD_HOA_DON_BAN.NGAY_BAN <= m_dtp_den_ngay.Value)
                && (x.GD_HOA_DON_BAN.HT_NGUOI_SU_DUNG.MA_NHAN_VIEN.Contains(m_txt_loc_du_lieu.Text)
                || x.GD_HOA_DON_BAN.HT_NGUOI_SU_DUNG.HO_TEN_NHAN_VIEN.Contains(m_txt_loc_du_lieu.Text)
                || m_txt_loc_du_lieu.Text.Equals("")
                    ))
                    .ToList();
            var v_lst_thong_ke = db.HT_NGUOI_SU_DUNG
                .OrderBy(x => x.MA_NHAN_VIEN)
                .ToList()
                .Select(x => new
                {
                    MA_NHAN_VIEN = x.MA_NHAN_VIEN,
                    HO_TEN_NHAN_VIEN = x.HO_TEN_NHAN_VIEN,
                    DOANH_SO = v_lst_chi_tiet_hoa_don_ban.Where(y => y.GD_HOA_DON_BAN.ID_NHAN_VIEN == x.ID).Sum(y => y.THANH_TIEN)
                })
                .OrderByDescending(x => x.DOANH_SO)
                .ToList();
            m_grv.DataSource = v_lst_thong_ke;

            m_grv.Columns[0].HeaderText = "Mã nhân viên";
            m_grv.Columns[0].Width = 120;
            m_grv.Columns[1].HeaderText = "Họ tên nhân viên";
            m_grv.Columns[1].Width = 200;
            m_grv.Columns[2].HeaderText = "Doanh số";
            m_grv.Columns[2].Width = 60;
            m_grv.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
        }
        #endregion

        #region Events
        private void F980_Thong_ke_doanh_thu_theo_nhan_vien_Load(object sender, EventArgs e)
        {
            load_data_to_grid();

            // Gán giá trị ban đầu cho datetime picker
            DateTime v_dat_dau_nam = new DateTime(DateTime.Now.Year, 1, 1);
            m_dtp_tu_ngay.Value = v_dat_dau_nam;
            m_dtp_den_ngay.Value = DateTime.Now;
        }

        private void m_cmd_quay_lai_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_xem_bao_cao_Click(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }
        #endregion
    }
}
